Add structural Protocol types for TSC item classes#1802
Conversation
…gs type - Rename DefaultPermissionsEndpoint's local BaseItem alias to DefaultPermissionsTarget to avoid shadowing the new public Protocol - Remove _initial_tags from TaggableItem (internal dirty-tracking detail, not a public contract); update ContentItem docstring to include MetricItem - Narrow WorkbookItem._initial_tags and DatasourceItem._initial_tags annotations from bare set to set[str] for Protocol invariance compliance Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… __all__ Taggable in resource_tagger.py was never used as a type bound -- TaggableItem in base_item.py now covers the public contract. Also remove runtime_checkable import which became unused. Fix pre-existing duplicate SiteOIDCConfiguration entry in models/__init__.__all__. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ViewItem.owner_id is not independently writable (it tracks the parent workbook's owner), so a plain writable Protocol attribute annotation would mislead mypy. A @Property annotation satisfies both ViewItem's read-only property and the writable instance attributes on other item classes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add test/test_protocols.py covering isinstance() checks for BaseItem, OwnedItem, TaggableItem, and ContentItem against all representative item classes (WorkbookItem, DatasourceItem, ViewItem, FlowItem, ProjectItem, MetricItem, UserItem) and plain structural objects, including negative cases and protocol-hierarchy checks. - Add _TaggableWithInitial private Protocol in resource_tagger.py to capture the _initial_tags implementation detail alongside the public TaggableItem surface; use it to fully annotate _ResourceTagger._add_tags, _delete_tag, and update_tags. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…test Add section comments to __init__.__all__ to distinguish the new structural protocols from the alphabetical concrete-model list. Move the inline `import datetime` in test_protocols.py to module level, matching the import style used throughout the test suite. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
This is great and will make other typing easier. Should they be named differently to clearly differentiate them? Something like |
|
Hm, I think underscores would make some more helpful IDEs hide/limit them in autocomplete etc, so not that. A common approach is something like "SupportsOwner", but that doesn't quite work for "ContentItem". I think the current item works ok and is pretty standard. |
…es.py
BaseItem is renamed to TableauItem to match the existing name in
tableau_types.py and fulfill its TODO comment ("should define TableauItem
as an interface"). The Union type alias is replaced with an import of the
Protocol, removing the need to enumerate concrete types.
id and name are now declared as @Property on the Protocol so that concrete
classes with read-only property implementations (and VirtualConnectionItem
whose name: str is narrower than str | None) satisfy it under mypy's
covariant property checking.
A private _PermissibleItem Protocol is added to permissions_endpoint.py
to type the populate() method's _set_permissions call, which is an
implementation detail not appropriate for the public TableauItem Protocol.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Apparently an underscore prefix can get them ignored by some IDEs, so not that. I spent a while looking at other projects and it seems the standard two choices are "Thingable/SupportsThing" for atomic Protocols (like SupportsInt and SupportsFloat) or just "Thing". I think the current naming is ok. |
Summary
base_item.pywith fourruntime_checkableProtocol classes:BaseItem(id, name),OwnedItem(+ owner_id),TaggableItem(+ tags), andContentItem(+ created_at, updated_at), forming a composable hierarchytableauserverclient.modelswith section comments separating them from the concrete model listTaggableprotocol inresource_tagger.pywith a private_TaggableWithInitialand fully annotates all_ResourceTaggermethod signaturesBaseItemtype alias indefault_permissions_endpoint.pytoDefaultPermissionsTargetto avoid shadowing the new public Protocol_initial_tags: settoset[str]inDatasourceItemandWorkbookItemfor Protocol invariance complianceSiteOIDCConfigurationentry frommodels.__all__test/test_protocols.pySchema compliance
All five primary content types in ts-api_3_29.xsd (workbookType, dataSourceType, viewType, flowType, metricType) carry id, name, owner, tags, and createdAt/updatedAt attributes. The Protocol hierarchy matches the schema exactly. MetricItem correctly satisfies ContentItem per schema confirmation.
Test plan
python -m pytest test/test_protocols.py -v-- 41 tests, all passfrom tableauserverclient.models import BaseItem, OwnedItem, TaggableItem, ContentItemworksisinstance(TSC.WorkbookItem(...), ContentItem)returnsTrueat runtime🤖 Generated with Claude Code